package net.adetech.crj.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.Option;
import net.adetech.crj.bean.Paper;
import net.adetech.crj.dao.DDMapper;
import net.adetech.crj.utils.ApiUtils;
import net.adetech.crj.utils.InnerServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("paper.getListBySfzh")
public class GetPaperListBySfzhService implements BaseService {

    @Autowired
    ApiUtils apiUtils;

    @Autowired
    DDMapper ddMapper;

    public static final String[] zjzlArray = {"14", "21", "25"};

    public static Map<String, String> qfddMap = new HashMap<String, String>();

    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        if (qfddMap.size() <= 0) {
            List<Option> optionList = ddMapper.getOptionList("908");
            if (optionList != null && optionList.size() > 0) {
                for (Option option : optionList) {
                    qfddMap.put(option.getKey(),option.getValue());
                }
            }
        }

        String sfzh = (String) params.get("sfzh");
        JSONObject args = new JSONObject();
        args.put("sfzh", sfzh);

        List<Paper> paperList = new ArrayList<Paper>();
        for (String zjzl : zjzlArray) {
            args.put("zjzl", zjzl);
            JSONObject jsonObject = apiUtils.call("zj_queryPaper", args);
            int success = jsonObject.getInteger("success");
            if (success == 1) {
                JSONArray paperListJson = jsonObject.getJSONObject("data").getJSONArray("papers");

                for (int i = 0; i < paperListJson.size(); i++) {
                    JSONObject paperJson = paperListJson.getJSONObject(i);
                    String qfrq = paperJson.getString("qfrq");
                    String csrq = paperJson.getString("csrq");
                    String zjzt = paperJson.getString("zjzt");
                    String zjyxqz = paperJson.getString("zjyxqz");
                    String qfjg = paperJson.getString("qfjg");
                    String xb = paperJson.getString("xb");
                    String zwxm = paperJson.getString("zwxm");
                    String zjhm = paperJson.getString("zjhm");
                    String qfdd = qfddMap.get(qfjg);

                    Paper paper = new Paper();
                    paper.setSfzh(sfzh);
                    paper.setZjzl(zjzl);
                    paper.setQfrq(qfrq);
                    paper.setQfjg(qfjg);
                    paper.setCsrq(csrq);
                    paper.setZjzt(zjzt);
                    paper.setZjyxqz(zjyxqz);
                    paper.setXb(xb);
                    paper.setZwxm(zwxm);
                    paper.setZjhm(zjhm);
                    paper.setQfdd(qfdd);
                    paperList.add(paper);
                }

            }
        }
        return InnerServiceUtil.buildResult("paperList", paperList);
    }
}
